package com.baizhi;

import com.baizhi.entity.Article;
import com.baizhi.service.ArticleService;
import com.baizhi.service.LuceneService;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 野马 on 2019/6/26.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = CmfzApplication.class)
public class LuceneTest2 {

    @Autowired
    private ArticleService articleService;

    @Test
    public void insert() throws IOException {
        //查询数据
        List<Article> articleList = articleService.selectArticleAll(null);
        List<Document> list = new ArrayList<>();

        //把数据封装在Document中
        for (Article article : articleList) {
            Document document = new Document();
            document.add(new TextField("articleId", article.getArticleId().toString(), Field.Store.YES));
            document.add(new TextField("articleName", article.getArticleName(), Field.Store.YES));
            document.add(new StringField("articleContent", article.getArticleContent(), Field.Store.YES));
            list.add(document);
        }
        //索引存放的位置
        FSDirectory directory = FSDirectory.open(new File("E://Test"));

        //定义分词器
        IKAnalyzer analyzer = new IKAnalyzer();
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_44,analyzer);
        //将数据持久化    实际就是流
        IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);
        //将封装的数据  存到索引苦衷
        indexWriter.addDocuments(list);

        //提交
        indexWriter.commit();

        indexWriter.close();

    }

    @Test
    public void select() throws Exception {
        //准备关键字
        String keywork = "上师";

        //处理关键字
        String[] files = {"articleName","articleContent"};

        MultiFieldQueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_44,files,new IKAnalyzer());
        Query query = queryParser.parse(keywork);

        //用处理好的关键字查索引
        FSDirectory directory = FSDirectory.open(new File("E://Test"));
        DirectoryReader directoryReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(directoryReader);

        //4通过查到索引中的位置信息  获取数据
        TopDocs topDocs = indexSearcher.search(query,10);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;

        for(int i=0;i<scoreDocs.length;i++){
            ScoreDoc scoreDoc = scoreDocs[i];
            int docId = scoreDoc.doc;
            Document document = indexSearcher.doc(docId);
            String articleId = document.get("articleId");
            String articleName = document.get("articleName");
            String articleContent = document.get("articleContent");
            System.out.println(articleId+"   "+articleName+"    "+articleContent);
        }
    }

    @Test
    public void delete() throws IOException {
        FSDirectory directory = FSDirectory.open(new File("E://Test"));
        IKAnalyzer analyzer = new IKAnalyzer();
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_44,analyzer);
        IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);
        indexWriter.deleteAll();
        indexWriter.commit();
        indexWriter.close();
    }

    @Test
    public void deleteOne() throws IOException {
        FSDirectory directory = FSDirectory.open(new File("E://Test"));
        IKAnalyzer analyzer = new IKAnalyzer();
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_44,analyzer);
        IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);
        indexWriter.deleteDocuments(new Term("articleId","2"));
        indexWriter.commit();
        indexWriter.close();
    }

    @Autowired
    private LuceneService luceneService;

    @Test
    public void selectService() throws IOException, ParseException, InvalidTokenOffsetsException {

        List<Article> articleList = luceneService.luceneSeleteByKeyword("上师");
        System.out.println(articleList.size());
        for (Article article : articleList) {
            System.out.println(article);

        }
    }

    @Test
    public void deleteLucene() throws IOException {
        luceneService.deleteAllIndex();
    }

    @Test
    public void addLucene() throws IOException {
        luceneService.addOneDocument(null);

    }

    @Test
    public void selectLucene() throws ParseException, InvalidTokenOffsetsException, IOException {
        List<Article> articleList = luceneService.luceneSeleteByKeyword("上师");

        for (Article article : articleList) {
            System.out.println(article);
        }
    }






}
